package cn.shequren.communityPeople.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.jz.community.basecomm.constant.AppConstants;
import com.jz.community.basecomm.utils.LoggerUtils;
import com.jz.community.basecomm.utils.WpToast;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import static com.tencent.mm.opensdk.modelbase.BaseResp.ErrCode.ERR_AUTH_DENIED;

/**
 * Created by Tony.Fan on 2018/3/23 11:26
 */
public class WXEntryActivity extends Activity implements IWXAPIEventHandler{

    private static int RESPONSE_TYPE_AUTHORIZE = 1;
    private IWXAPI api;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        api = WXAPIFactory.createWXAPI(this, AppConstants.WX_APP_ID);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    public void onReq(BaseReq baseReq) {
        LoggerUtils.fLog().i("微信回调类型：" + baseReq.getType());
    }

    @Override
    public void onResp(BaseResp baseResp) {

        LoggerUtils.fLog().i("微信回调类型：" + baseResp.getType());
        LoggerUtils.fLog().i("微信回调状态码：" + baseResp.errCode);


        //小程序
        if (baseResp.getType() == ConstantsAPI.COMMAND_LAUNCH_WX_MINIPROGRAM) {
            finish();
            return;
        }

        //微信授权
        if (baseResp.getType() == RESPONSE_TYPE_AUTHORIZE) {
            handelSendAuthInfo(baseResp);
            return;
        }

        //处理分享
        switch (baseResp.errCode) {
            case BaseResp.ErrCode.ERR_OK:
                WpToast.l(WXEntryActivity.this, "分享成功");
                finish();
                break;
            case BaseResp.ErrCode.ERR_USER_CANCEL:
            case ERR_AUTH_DENIED:
            default:
                WpToast.l(WXEntryActivity.this, "分享失败");
                finish();
                break;
        }
    }

    /**
     * 联合登录
     *
     * @param baseResp
     */
    private void handelSendAuthInfo(BaseResp baseResp) {
        if (baseResp instanceof SendAuth.Resp) {
            SendAuth.Resp resp = (SendAuth.Resp) baseResp;
            Intent intent = new Intent();
            intent.putExtra("code", resp.code);
            intent.putExtra("openId", baseResp.openId);
            intent.setAction(AppConstants.WEIXIN_LOGIN_INTENT_ACTION);
            sendBroadcast(intent);
            finish();
        }
    }
}
